﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NetworkManagement;

namespace LorraineSec.MessageLib.Messages
{
    public sealed class SubscribeDeviceRequest : EncryptedMessage
    {
        #region Format Constants
        /*
         * Format:
         *        1              x
         * |DeviceSerialLen|DeviceSerial|
         */

        //Lengths
        private const int ENC_DEVICESERIALLEN_LENGTH = 1;

        //Offsets
        private const int ENC_DEVICESERIALLEN_OFFSET = 0;
        private const int ENC_DEVICESERIAL_OFFSET = ENC_DEVICESERIALLEN_OFFSET + ENC_DEVICESERIALLEN_LENGTH;

        //Known Length
        private const int ENC_KNOWN_LENGTH = ENC_DEVICESERIALLEN_LENGTH;

        #endregion

        public string DeviceSerial;

        public SubscribeDeviceRequest()
        {
            init();
        }

        public SubscribeDeviceRequest(byte[] bytes)
            : base(bytes)
        {
            init();
            decode();
        }

        private void init()
        {
            MessageID = (ushort)LorraineSecMessageFactory.MessageID.SUBSCRIBE_DEVICE_REQUEST;
        }

        public override void decrypt(CryptoManager crypt)
        {
            //Decrypt byte array
            base.decrypt(crypt);
            //Decode byte array
            DeviceSerial = Bytes.getString(encData, ENC_DEVICESERIAL_OFFSET, Bytes.getByte(encData, ENC_DEVICESERIALLEN_OFFSET));
        }

        public override void encrypt(CryptoManager crypt)
        {
            //Encode Byte Array
            encData = new byte[ENC_KNOWN_LENGTH + DeviceSerial.Length];
            Bytes.setValue(encData, ENC_DEVICESERIALLEN_OFFSET, (byte) DeviceSerial.Length);
            Bytes.setValue(encData, ENC_DEVICESERIAL_OFFSET + DeviceSerial.Length, DeviceSerial);
            //Encrypt Byte Array
            base.encrypt(crypt);
        }

    }
}
